#include "Core.h"
#include "Base.h"
#include "RenderManager.h"
#include "InputManager.h"
#include "Exceptions\Exception.h"
#include "Cameras\Camera.h"
#include "RenderableObjects\StaticMesh\StaticMeshManager.h"
#include "Textures\TextureManager.h"

#if defined (_DEBUG)
#include "Memory/MemLeaks.h"
#endif

//-------------------------------------------------
CCore::CCore()
	: m_pRenderManager(NULL)
	, m_pInputManager(NULL)
	, m_pStaticMeshManager(NULL)
	, m_pTextureManager(NULL)
	, m_pCamera(NULL)
{

}

//-------------------------------------------------
CCore::~CCore()
{
	CHECKED_DELETE(m_pRenderManager);
	CHECKED_DELETE(m_pInputManager);
	CHECKED_DELETE(m_pStaticMeshManager);
	CHECKED_DELETE(m_pTextureManager);
}

//-------------------------------------------------
void CCore::Initialize(HWND _hWnd)
{
	bool l_bIsOk = true;

	m_pRenderManager = new CRenderManager();
	l_bIsOk = m_pRenderManager->Initialize(_hWnd);
	if(!l_bIsOk)
		throw CException(__FILE__, __LINE__, "Error initializing DirectX 11." );

	m_pInputManager = new CInputManager();
	m_pInputManager->Initialize(_hWnd, m_pRenderManager->GetScreenSize());
	m_pInputManager->Load("./data/xml/input.xml");

	m_pStaticMeshManager = new CStaticMeshManager();
	m_pStaticMeshManager->Load("./data/xml/static_meshes.xml");

	m_pTextureManager = new CTextureManager();
	m_pTextureManager->AddTexture("./data/textures/seafloor.dds");

	m_pRenderManager->CreateCube();
	//m_pRenderManager->CreateTriangle();
}

//-------------------------------------------------
void CCore::Update(float _fElapsedTime)
{
	m_pInputManager->Update();
}

//-------------------------------------------------
void CCore::Render()
{
	m_pRenderManager->ClearTarget();
	m_pRenderManager->SetupMatrices(m_pCamera);
	//m_pRenderManager->RenderCube();
	//m_pRenderManager->RenderTriangle();

	m_pStaticMeshManager->Render(m_pRenderManager);

	m_pRenderManager->EndRendering();
}